//## 가공(2팀) 정보 저장 ####################
function fnSave()
{
	if (!$("#prdNm").val())
	{
		alert("제품명을 바르게 입력해 주세요!");
		$("#prdNm").focus();
		return;
	}
	var valUrl  = '/action/extraOrder/save.asp';
	var param  = "prdNm="+encodeURIComponent($("#prdNm").val());
		param += "&workUid="+$("#workUid").val();
		param += "&remarks="+encodeURIComponent($("#remarks").val());
		param += "&planNm="+encodeURIComponent($("#planNm").val());
		param += "&compCd="+$("#compCd").val();
		param += "&source="+$("#source").val();
		param += "&afterDeal="+$("#afterDeal").val();
		param += "&flag="+$("#flag").val();
		param += "&workDt="+$("#workDt").val();
		param += "&outDt="+$("#outDt").val();
		param += "&lotSeq="+$("#lotSeq").val();
		param += "&qty="+$("#qty").val();
	
	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			fnSessionCheck($(xml).find("MSG").text());
			if ($(xml).find("MSG").text() == "Save OK!")
			{
				alert("Save OK!");
				fnReset();
				grid01List();
			}
			else alert($(xml).find("MSG").text());
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}


//## 가공(2팀) 정보 그리드 표시 ####################
var G_curPage01 = 1;

function grid01List(page)
{
	
	var gridNo = 1;
	var xmlNode = "OrderList";

	fnResetGrid(gridNo);
	fnNewLoading(gridNo);

	if (!page) page = G_curPage01;
	if (page == undefined) page = 1;
	else G_curPage01 = page;
	var limit = 50;

	var valUrl = "/action/extraOrder/list.asp";
	var Parm = "findKeyset="+$("#findKeyset").val();
		Parm += "&findStartDt="+$("#findStartDt").val();
		Parm += "&findEndDt="+$("#findEndDt").val();
		Parm += "&findPrdNm="+encodeURIComponent($("#findPrdNm").val());
	if (xMode=="Debug") window.open(valUrl+'?xMode=debug&'+Parm);

	// 그리드 헤더를 기준으로 Cell의 폭을 구해온다.
	var cellWidth = fnGetGridCellWidth(gridNo);	
	// Cell의 정렬방식
	var cellAlign = ",,,,,,,,,left,,".split(",");
	// Cell의 링크 걸지 여부 결정
	var cellLink = ",,,,,,,,,,".split(",");

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: Parm,
		success: function(xml){
			var gridList = "";
			var k = 1;
			var ttlCnt = $(xml).find(xmlNode).length;
			var startNo = (parseInt(page,10)-1) * limit;
			var endNo = parseInt(page,10) * limit;
			var bgColor = "#FFF";
			var chkCnt = 0;
			
			// 그리드의 부가 정보(레코드수, 페이징) 표시 또는 가리기
			fnGridExtraInfo(gridNo, ttlCnt, page, limit); 

			if (ttlCnt <= endNo) endNo = ttlCnt;
			var no = endNo;
			var ttlGood = 0;
			var ttlBad = 0;
			var ttlQty = 0;
			$(xml).find(xmlNode).each(function(idx){
				
				if (k>startNo && k<=endNo)
				{
					var lotSeq		= $(this).find('lotSeq').text();
					var prdNm		= $(this).find('prdNm').text();
					var workDt		= $(this).find('workDt').text();
					var outDt			= $(this).find('outDt').text();
					var company	= $(this).find('company').text();
					var qty				= $(this).find('qty').text();
					var flag			= $(this).find('flag').text();
					
					var vFlag = "생산중";
					if (flag == "N") 
					{
						vFlag = "삭제";
						bgColor = "#F2F2F2";
					}
					else if (flag == "H") 
					{
						vFlag = "중지";
						bgColor = "#f0faff";
					}
					else if (flag == "C") 
					{
						vFlag = "완료";
						bgColor = "#ffecff";
					}
					else  bgColor = "#fff";
					
					var arrVal = new Array();
					arrVal[0] = "<p>"+no+"</p>";
					arrVal[1] = "<p>"+prdNm+"</p>";
					arrVal[2] = "<p>"+workDt+"</p>";
					arrVal[3] = "<p>"+outDt+"</p>";
					arrVal[4] = "<p>"+company+"</p>";
					arrVal[5] = "<p>"+qty+"</p>";
					arrVal[6] = "<p>"+vFlag+"</p>";					

					var link = "fnSetOrderDetail('"+lotSeq+"')";

					gridList = fnDrawCell(arrVal, bgColor, cellWidth, cellAlign, '', link, cellLink);

					$("#"+"gridBody"+gridNo).append(gridList);
				}
				no = parseInt(endNo,10)-parseInt(k,10);
				k++;			
			});
		},
		error: function(xml){
			alert('Error!!\r\n'+xml.responseText);
		}
	});

}

//## 가공(2팀) 상세정보 표시 ####################
function fnSetOrderDetail(lotSeq)
{
	var valUrl = "/action/extraOrder/view.asp";
	var param = "lotSeq="+lotSeq;
	
	fnReset();

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());

			fnFindCompany('',$(xml).find("compCd").text(),'compCd');		
			$("#lotSeq").val($(xml).find("lotSeq").text());
			$("#prdNm").val($(xml).find("prdNm").text());
			$("#workUid").val($(xml).find("workUid").text());
			$("#workDt").val($(xml).find("workDt").text());
			$("#planNm").val($(xml).find("planNm").text());
			$("#outDt").val($(xml).find("outDt").text());
			$("#remarks").val($(xml).find("remarks").text());
			$("#qty").val($(xml).find("qty").text());
			$("#source").val($(xml).find("source").text());
			$("#afterDeal").val($(xml).find("afterDeal").text());
			$("#flag").val($(xml).find("flag").text());
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 가공(2팀) 정보 Reset 기능
function fnReset()
{
	$("#lotSeq").val("");
	$("#prdNm").val("");
	$("#compCd").val("");
	$("#compNm").val("");
	$("#workUid").val("");
	$("#remarks").val("");
	$("#planNm").val("");
	$("#source").val("");
	$("#afterDeal").val("");
	$("#qty").val("");
	$("#flag").val("Y");
}

//## 검색 Reset 기능
function fnSearchReset()
{
	$("#findPrdNm").val("");
}